<?php
/**
 * Created by PhpStorm.
 * User: 97606
 * Date: 2019/1/5
 * Time: 16:57
 */
namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\Article\ApiArticleClassify;
use App\Modules\Article\ApiArticle;
use Illuminate\Http\Request;

class Article extends AdapterWebController
{
    // 文章主题列表
    public function themeList()
    {
        $list = $this->appLayerExec(ApiArticleClassify::class, 'classifyList');

        return view('newAdmin.article.theme_list', ['list' => $list['res']]);
    }

    // 添加文章主题
    public function addTheme(Request $request)
    {
        $postData['name'] = $request['name'];
        $postData['user_id'] = session('userId');
        $postData['belong_to'] = $request['belong_to'];

        $res = $this->appLayerExec(ApiArticleClassify::class, 'add', $postData);

        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '主题增加成功!'];
        }

        return ['status' => -1, 'msg' => '主题增加失败，请重试!'];
    }

    // 编辑文章主题
    public function editTheme(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            $postData['id'] = (int)$id;
            $res = $this->appLayerExec(ApiArticleClassify::class, 'classifyInfo', $postData);
            if ($res['code'] == 200) {
                return view('newAdmin.article.theme_edit', ['info' => $res['res']]);
            }
            return false;
        }

        $postData = $request->all();
        $postData['id'] = $id;
        $res = $this->appLayerExec(ApiArticleClassify::class, 'edit', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '主题保存成功!'];
        }
        return ['status' => -1, 'msg' => '主题信息修改异常，请重试!'];
    }

    // 文章列表
    public function articleList(Request $request)
    {
        if (empty($request->all())) {
            $postData['status'] = 1;
            $list = $this->appLayerExec(ApiArticle::class, 'list', $postData);
        } else {
            $postData = $request->all();
            $postData['status'] = 1;
            $list = $this->appLayerExec(ApiArticle::class, 'list', $postData);
        }

        return view('newAdmin.article.article_list', ['list' => $list['res']]);
    }

    // 添加文章
    public function addArticle(Request $request)
    {
        if ($request->isMethod('get')) {
            $themeList = $this->appLayerExec(ApiArticleClassify::class, 'themeList');

            return view('newAdmin.article.article_add', ['themeList' => $themeList['res']]);
        }

        $postData = $request->all();

        $res = $this->appLayerExec(ApiArticle::class, 'add', $postData);

        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '文章增加成功!'];
        }

        return ['status' => -1, 'msg' => '文章增加失败，请重试!'];
    }

    // 编辑文章
    public function editArticle(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            $themeList = $this->appLayerExec(ApiArticleClassify::class, 'themeList');
            $postData['id'] = (int)$id;
            $res = $this->appLayerExec(ApiArticle::class, 'info', $postData);
            if ($res['code'] == 200) {
                return view('newAdmin.article.article_edit', ['info' => $res['res'], 'themeList' => $themeList['res']]);
            }
            return false;
        }

        $postData = $request->all();
        $postData['id'] = $id;
        $res = $this->appLayerExec(ApiArticle::class, 'edit', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '文章保存成功!'];
        }
        return ['status' => -1, 'msg' => '文章信息修改异常，请重试!'];
    }

    // 删除文章
    public function delArticle($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiArticle::class, 'del', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '文章删除成功!'];
        }
        return ['status' => -1, 'msg' => '文章删除异常，请重试!'];
    }

    // 富文本编辑器文件(图片)上传
    public function files(Request $request)
    {
        $fileRes = $this->fileUpload($request, 'files');
        if (!$fileRes['status']) {
            $imageInfo = [
                "errno" => -1,
                "data" => []
            ];

            return json_encode($imageInfo);
        }
        $imageInfo = [
            "errno" => 0,
            "data" => [
                $fileRes['url']
            ]
        ];

        return json_encode($imageInfo);
    }
}
